package com.youdianda.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.youdianda.context.BaseContext;
import com.youdianda.dto.UserDataHandleDTO;
import com.youdianda.entity.YddUserData;
import com.youdianda.mapper.YddUserDataMapper;
import com.youdianda.result.Result;
import com.youdianda.service.IYddUserDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sdy
 * @since 2023-05-15
 */
@Service
public class YddUserDataServiceImpl extends ServiceImpl<YddUserDataMapper, YddUserData> implements IYddUserDataService {

    /**
     * GET 点赞/取消点赞和收藏/取消收藏
     *
     * @param userDataHandleDTO
     * @return
     */
    @Override
    public Result userDataHandle(UserDataHandleDTO userDataHandleDTO) {

        YddUserData yddUserData = YddUserData.builder().articleId(userDataHandleDTO.getArticle_id())
                .type(userDataHandleDTO.getType())
                .userId(Integer.parseInt(BaseContext.getCurrentId() + "")).build();

        //添加点赞或者收藏
        if(userDataHandleDTO.getAction().equals("add")){
            save(yddUserData);
        }

        //取消点赞或者收藏
        if(userDataHandleDTO.getAction().equals("del")){
            remove(Wrappers.<YddUserData>lambdaQuery().eq(YddUserData::getUserId,BaseContext.getCurrentId())
                    .eq(YddUserData::getArticleId,userDataHandleDTO.getArticle_id()));
        }

        return Result.success();
    }
}
